﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScanDevConsole
{
    public class SwitchButton : System.Windows.Forms.Button
    {
        public bool isOn = false;
        public event EventHandler ToggledEvent;     //已经切换状态事件
        public SwitchButton() : base()
        {
            //this.Size = new System.Drawing.Size(50, 30);
            FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            FlatAppearance.BorderSize = 1;
            FlatAppearance.BorderColor = System.Drawing.Color.Black;
            FlatAppearance.MouseDownBackColor = System.Drawing.Color.DarkGreen;
            //FlatAppearance.MouseOverBackColor = System.Drawing.Color.LightBlue;
            UseVisualStyleBackColor = true;
            BackColor = System.Drawing.Color.DarkRed;

            MouseDown += new System.Windows.Forms.MouseEventHandler(onBtnDown);
            MouseUp += new System.Windows.Forms.MouseEventHandler(onBtnUp);
        }


        private void onBtnDown(object sender, MouseEventArgs e)
        {
            isOn = !isOn;
            if (isOn)
            {
                BackColor = System.Drawing.Color.LightGreen;
            }
            else
            {
                BackColor = System.Drawing.Color.DarkRed;
            }
            EventHandler handler = ToggledEvent;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        private void onBtnUp(object sender, MouseEventArgs e)
        {

        }
    }
}
